<?php
/**
 * Status
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/2/7
 * Time: 22:22
 */

namespace app\admin\validate;

use app\common\lib\StatusLib;
use think\Validate;

class Status extends Validate
{
    protected $rule = [
        'id'                     => 'require|integer',
        'status'                 => "require|checkStatus",
        'is_index_recommend'     => "require|checkStatus",
        'listorder'              => 'require|integer',
    ];
    protected $message = [
        'id.require'     => 'id不能为空',
        'id.integer'     => 'id必须为整数',
        'status.require' => '状态参数不能为空',
        'is_index_recommend.require' => '状态不能为空',
//        'status.in'      => '状态参数不合法',
    ];
    protected $scene = [
        'status'              => ['id','status'],
        'is_index_recommend'  => ['id','is_index_recommend'],
        'listorder'           =>['id','listorder']
    ];
    /**
     * 自定义规则——验证是否在指定范围内（相当于in,in不支持变量）
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    protected function checkStatus($value,$rule,$data){
        return in_array($value,StatusLib::getTableStatus()) ? true : '状态参数不合法';
    }
}
